package com.atguigu.gulimail.ware.service.impl;

import com.atguigu.common.utils.Response;
import com.atguigu.gulimail.ware.Vo.SkuHasStockVo;
import com.atguigu.gulimail.ware.fegin.WareProductFegin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.ware.dao.WareSkuDao;
import com.atguigu.gulimail.ware.entity.WareSkuEntity;
import com.atguigu.gulimail.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    WareProductFegin wareProductFegin;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        /**
         *    wareId: 123,//仓库id
         *    skuId: 123//商品id
         */
        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            queryWrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }


        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),queryWrapper

        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock (Long skuId, Long wareId, Integer skuNum) {
        //1.判断如果如果没有这个库存记录,就是新增操作
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id", skuId).eq("ware_id", wareId)
        );
        if(entities==null||entities.size()==0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //调用远程服务查出sku的名字 如果失败 整个事务无需回滚
            //1.自己catch异常
            //todo 高级部分
              try {
                  Response info = wareProductFegin.info(skuId);
                  Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                  if(info.getCode()==0){
                      wareSkuEntity.setSkuName((String) data.get("skuName"));
                  }
              } catch(Exception e) {
                  wareSkuEntity.setSkuName("请重新设置sku名称");
              }
            wareSkuDao.insert(wareSkuEntity);
        }else {
            //2.如果有,就修改
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }



    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock (List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(sku -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();

            //查询当前sku总库存量
            Long count = baseMapper.getSkuStock(sku);
            skuHasStockVo.setSkuId(sku);
            skuHasStockVo.setHasStock(count!=null?true:false);
            return skuHasStockVo;
        }).collect(Collectors.toList());

        return collect;
    }

}